#!/usr/bin/python
import sys
import os
import pexpect

def expectandshow(child, expect):
        index=child.expect(expect)
        print child.before,child.after,
        return index


fp_intr = open('/root/vmimage/mpi_paas/code/ip','r')
order=[]
while True:
        line = fp_intr.readline()
        length = len(line)
        if length == 0: # Zero length indicates EOF
                break

        line=line[0:length-1]
        print line
        order.append(line)
fp_intr.close();

for ip in order:
	cmd="ssh root@" + ip + " echo '' > /etc/hosts.equiv;"
	for ip1 in order:
		cmd += "echo " +ip1 + " >> /etc/hosts.equiv;"
	cmd += "echo '' > /root/.rhosts;"
	for ip2 in order:
		cmd += "echo " +ip2 + " >> /root/.rhosts;"

	cmd += "/sbin/chkconfig rsh off;" + \
		"/sbin/chkconfig rsh on;" + \
		"/etc/init.d/xinetd start;"

	
	print cmd
	password="passw0rd"
	scp_child=pexpect.spawn(cmd)
	index=expectandshow(scp_child, \
                                ['Are you sure you want to continue connecting', \
                                "root@"+ip+"'s password:", \
                                pexpect.TIMEOUT, \
                                pexpect.EOF])
	if index == 0:
        	scp_child.sendline('yes')
       	 	ret = expectandshow(scp_child, ["root@"+ip+"'s password:",pexpect.EOF])
        	if ret == 0:
                	scp_child.sendline(password)
			expectandshow(scp_child, [pexpect.EOF])
                elif ret == 1:	
			pass
	elif index == 1:
        	scp_child.sendline(password)
		expectandshow(scp_child, [pexpect.EOF])

	elif index == 2:
        	print "timeout, ignore and continue."
	elif index == 3:
        	pass

